package dao;

import utildb.GenericDAO;
import utildb.Transaction;
import utildb.TransactionException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class NumeroVisitaDAO extends GenericDAO {

    public NumeroVisitaDAO(Transaction transacction) {
        super(transacction);
    }

    public double incrementarContador() throws TransactionException {
        double numero = 0;
        try {
            int i = 1;
            Connection c;
            Statement statement = null;
            c = transaction.getConnection();
            statement = c.createStatement();

            String ccadSql = "select nVis_Nume from mySql.CSTC_VISI";
            ResultSet rs = statement.executeQuery( ccadSql );
            while (rs.next()) {
                numero = rs.getDouble("nVis_Nume");
            }
            if (numero == 0) {
                numero = 1;
            }
            else {
                numero = numero + 1;
            }
            ccadSql = "update mySql.CSTC_VISI SET nVis_Nume = nVis_Nume + 1";

            int n;
            n = statement.executeUpdate(ccadSql);
            statement.close();
            if (n == 0)
                        throw new TransactionException("No se pudo eliminar actualizar el contador");
        } catch (SQLException e1) {
                throw new TransactionException(e1);
        }
        return numero;
    }

    public void eliminar(String codigo) throws TransactionException {
        throw new UnsupportedOperationException("No soportado");
    }
}
